// pages/toDolist/toDolist.js
Page({
  data: {
    value:[
    ]
  },
  onLoad: function (options) {
    var value = wx.getStorageSync('value');
    if(value.length){
      this.setData({
        value
      })
    }else{
      wx.setStorageSync('value', [])
    }
  },
  onSearch(event){
    console.log(event.detail);
    let detail = event.detail.trim();
    let value = this.data.value
    if(value.every(item=>item.name!=detail)&&detail){
      value.push({
        name:event.detail,
        state:false
      })
    }else{
      wx.showToast({
        title: '数据已存在或者数据为空',
        icon: '/images/看过.png'
      })
    }
    wx.setStorageSync('value', value);
    this.setData({
      value
    })
  },
  onClick(event){
    let data = this.data.value;
    console.log(event);
    data.forEach((res,index)=>{
      console.log(index);
      if(index==event.currentTarget.dataset.id){
        res.state = event.detail
      }
    })
    wx.setStorageSync('value', data);
    this.setData({
      value:data
    })
  },
  onRemove(event){
    this.data.value.forEach((res,index)=>{
      if(index==event.currentTarget.dataset.id){
        this.data.value.splice(index,1)
      }
    })
    wx.setStorageSync('value', this.data.value)
    this.setData({
      value:this.data.value
    })
  }
})